package com.zhudeshuai.zhudemolist.activity;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.ActivityOptions;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

import com.zhudeshuai.zhudemolist.R;
import com.zhudeshuai.zhudemolist.base.BaseActivity;
import com.zhudeshuai.zhudemolist.bean.WeatherInfoResult;
import com.zhudeshuai.zhudemolist.http.RequestHelper;

public class RetrofitDemoActivity extends BaseActivity {

    private TextView mResultTv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_retrofit_demo);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });


        mResultTv = (TextView) findViewById(R.id.tv_result);

        findViewById(R.id.bt_get).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                invokeGet();
            }
        });
        findViewById(R.id.bt_post).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                invokePost();
            }
        });
    }

    private void invokePost() {

    }

    private void invokeGet() {
        RequestHelper.getInstance().requestWeather(new RequestHelper.IResponseCallback<WeatherInfoResult>() {
            @Override
            public void onSuccess(WeatherInfoResult weatherInfoResult) {
                mResultTv.setText(weatherInfoResult.toString());
            }

            @Override
            public void onFailed(Throwable throwable) {
                mResultTv.setText(throwable.getMessage());
            }
        });
    }


    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public static void open(Activity activity) {
        Intent intent = new Intent(activity, RetrofitDemoActivity.class);
        activity.startActivity(intent,
                ActivityOptions.makeSceneTransitionAnimation(activity).toBundle());
    }

}
